<?

if ($DATA['component']['id']) {

	$s = array('path' => 'gallery', 'tn' => FP_TN_DIR, 'w' => 800, 'h' => 600, 'tnw' => 128, 'tnh' => 96);
	
	if ($DATA['component']['settings']['path'])
		$s['path'] = createdir(FP_UPLOAD_PATH.$DATA['component']['settings']['path'].'/');
	if ((int)$DATA['component']['settings']['w'])
		$s['w'] = (int)$DATA['component']['settings']['w'];
	if ((int)$DATA['component']['settings']['h'])
		$s['h'] = (int)$DATA['component']['settings']['h'];
	if ((int)$DATA['component']['settings']['tnw'])
		$s['tnw'] = (int)$DATA['component']['settings']['tnw'];
	if ((int)$DATA['component']['settings']['tnh'])
		$s['tnh'] = (int)$DATA['component']['settings']['tnh'];

	createdir($s['path'].$s['tn']);
	

	if (($d = param('delete')) && preg_match('/^[0-9]+\.[a-z]{3}$/i',$d)) {	// *** delete photo
		
		@unlink($_SERVER['DOCUMENT_ROOT'].$s['path'].$d);
		@unlink($_SERVER['DOCUMENT_ROOT'].$s['path'].$s['tn'].$d);
	
		header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'delete')));
		exit;
	}


	$DATA['gallery'] = datafetch_array('gallery', $s['path']);	// load existing images list
	uksort($DATA['gallery'], 'fn_numeric_compare');
	
	switch (action(array('photo_add'))) {
	
		case 'photo_add':
	
				if ($max = parse_fname(end(array_keys($DATA['gallery']))))
					$max = (int)$max['name'] + 1;
				else
					$max = 1;
	
				if ($temp = takeJPG('image', $s['w'], $s['h'], $s['path']."$max.jpg", $s['tnw'], $s['tnh'], $s['path'].$s['tn']."$max.jpg"))
						$DATA['gallery']["$max.jpg"] = array('w' => $temp['w'], 'h' => $temp['h'], 'fn' => "$max.jpg", 'tn' => "$s[tn]$max.jpg");
						
				return false;
	}


	return false;

} else exit;

?>